import {
  isPhone,
  isEmail,
  isIP,
  isID,
  isDate,
  requirementRegexp
} from "@/utils/validate";
import { getImageFileSize } from "@/utils/imgTools";
import { inRange } from "lodash";
export const reValide = (form, field) => {
  form.validate(field);
};
export const required = (message = "此为必填项") => {
  return {
    required: true,
    message: message,
    trigger: "blur"
  };
};
export const requiredOnChange = (message = "此为必填项") => {
  return {
    required: true,
    message: message,
    trigger: "change"
  };
};
export const requiredArray = (message = "此为必填项") => {
  return {
    type: "array",
    required: true,
    message: message,
    trigger: "blur"
  };
};
export const phoneValid = (required = true, message = "电话号码格式错误") => {
  return {
    validator: (rule, value, callback) => {
      if (!isPhone(value)) {
        callback(new Error(message));
        return;
      }
      callback();
    },
    required,
    trigger: "blur"
  };
};
export const emailValid = (required = true, message = "邮箱格式错误") => {
  return {
    validator: (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error(message));
        return;
      }
      callback();
    },
    required,
    trigger: "blur"
  };
};
export const ipValid = (required = true, message = "IP地址格式错误") => {
  return {
    validator: (rule, value, callback) => {
      if (value && !isIP(value)) {
        callback(new Error(message));
        return;
      }
      callback();
    },

    required,
    trigger: "blur"
  };
};
export const idValid = (required = true, message = "身份证格式错误") => {
  return {
    validator: (rule, value, callback) => {
      if (!isID(value)) {
        callback(new Error(message));
        return;
      }
      callback();
    },
    required,
    trigger: "blur"
  };
};

export const passwordLimit = (required = true, len = 8) => {
  return {
    validator: (rule, value, callback) => {
      if (String(value).length < len) {
        callback(
          new Error(`长度在${len}字以上,当前为${String(value).length}字`)
        );
        return;
      }
      if (!requirementRegexp(value)) {
        callback(new Error(`请确保包含数字、字母大小写、特殊字符!`));
        return;
      }
      callback();
    },
    required,
    trigger: "blur"
  };
};
export const lengthLimit = (required = true, len = 10) => {
  return {
    validator: (rule, value, callback) => {
      if (String(value).length > len) {
        callback(
          new Error(`长度在${len}字以内,当前为${String(value).length}字`)
        );
        return;
      }
      callback();
    },
    required,
    trigger: "blur"
  };
};
export const dateValid = (
  required = true,
  dateFormat = "",
  message = "日期格式错误"
) => {
  return {
    validator: (rule, value, callback) => {
      if (!value) {
        callback();
      }

      if (!isDate(value, dateFormat)) {
        callback(new Error(message));
        return;
      }
      callback();
    },
    required,
    message,
    trigger: "blur"
  };
};
export const dateArrayValid = (required = true, message = "日期格式错误") => {
  return {
    validator: (rule, value, callback) => {
      if (!Array.isArray(value)) {
        callback(new Error(message));
        return;
      }
      if (required && !value.length) {
        callback(new Error("需要填写日期"));
        return;
      }
      value.forEach(item => {
        if (!isDate(item)) {
          callback(new Error(message));
          return;
        }
      });

      callback();
    },
    required,
    message,
    trigger: "blur"
  };
};

export const imgSizeValid = ({ min, max } = {}) => {
  min = min ? min : 10;
  max = max ? max : 200;
  const message = `图片大小要求:大于${min}k,小于${max}k`;
  return {
    validator: (rule, value, callback) => {
      if (!value) {
        callback();
        return;
      }
      getImageFileSize(value)
        .then(size => {
          if (size == 776) {
            callback(new Error("图片加载失败"));
          } else if (inRange(size / 1000, min, max)) {
            callback();
          } else {
            callback(new Error(message));
          }
        })
        .catch(err => {
          callback(err);
        });
    },
    required,
    trigger: "change"
  };
};

export const rangeOutDated = (
  required = true,
  message = "当前时间段已过期"
) => {
  return {
    validator: (rule, value, callback) => {
      if (!Array.isArray(value) || !value[1]) {
        callback(new Error("填写的不是时间段"));
        return;
      }
      if (new Date() > new Date(value[1])) {
        callback(new Error("当前时间段已过期"));
      }

      callback();
    },
    required,
    message,
    trigger: "blur"
  };
};
